<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button type="warning" @click="addDiamond" size="small" icon="el-icon-plus">添加钻石</el-button></div>
    </div>
    <div class="mt-2" style="width: 60%;">
      <el-table :data="diamondList" border stripe>
        <el-table-column align="center" label="序号" type="index" width="45" />
        <el-table-column align="center" label="钻石数量" prop="diamond_number" width="160" />
        <el-table-column align="center" label="排序" prop="sort" width="60" />
        <el-table-column align="center" label="添加时间" prop="created_time" width="160" />
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="添加分类">
      <el-form :model="formData" ref="form" label-width="80px">
        <el-form-item label="钻石数量" prop="diamond_number">
          <div style="width: 50%;">
            <el-input v-model="formData.diamond_number"></el-input>
          </div>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <div style="width: 50%;">
            <el-input v-model="formData.sort"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    diamondpurchaseDeleteUrl,
    diamondpurchaseUpdateUrl,
    diamondpurchaseCreateUrl,
    diamondpurchaseDetailUrl,
    diamondpurchaseListUrl,
  } from '@/utils/interface'
  export default {
    data() {
      return {
        dialogVisible: false,
        addStatus: true,
        diamondList: [],
        formData: {
          diamond_number: 0,
          sort: 1
        },
        getDiamondId:''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getDiamondpurchaseList()
      },
      getDiamondpurchaseList() {
        this.$get(diamondpurchaseListUrl).then(res => {
          if (res.data.code == 200) {
            this.diamondList = res.data.data.list
          }
        })
      },
      addDiamond() {
        this.dialogVisible = true
        this.addStatus = true
      },
      submitForm() {
        if (this.addStatus) {
          this.$post(diamondpurchaseCreateUrl, this.formData).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              });
              this.getDiamondpurchaseList()
              this.dialogVisible = false
              this.formData = {}
            }
          })
        } else {
          this.$put(diamondpurchaseUpdateUrl + this.getDiamondId + '/', this.formData).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              });
              this.getDiamondpurchaseList()
              this.dialogVisible = false
              this.formData = {}
            }
          })
        }

      },
      handleEdit(diamondId) {
        this.dialogVisible = true
        this.addStatus = false
        this.$get(diamondpurchaseDetailUrl + diamondId + '/').then(res => {
          if (res.data.code == 200) {
            this.getDiamondId = res.data.data.id
            this.formData = {
              diamond_number: res.data.data.diamond_number,
              sort: res.data.data.sort
            }
          }
        })
      },
      openDelete(diamondId) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(diamondpurchaseDeleteUrl + diamondId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getDiamondpurchaseList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style>
</style>
